﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Util.Setvices.Model;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Util.Setvices.Controllers
{
    [Route("[controller]")]
    public class YNDictController : Controller
    {
        private IFreeSql _fsql;
        public YNDictController(IFreeSql fsql)
        {
            _fsql = fsql;
        }
        /// <summary>
        /// 获取字典列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public object lg_getDictList(string type, int parentId)
        {

            return _fsql.Select<YNDict>()
                .Where(a => a.type == type && a.parentId == parentId)
                .OrderBy(a => a.serId)
                .ToList(a => new
                {
                    a.serId,
                    a.title
                });
        }


        [HttpGet]
        [Route("search")]
        public object lg_getDictSearchList(string type, string searchKey, int level)
        {
           
            if (string.IsNullOrEmpty(searchKey))
            {
                return new List<YNDict>();
            }
            var list = _fsql.Select<YNDict>()
                .Where(a => a.type == type && a.level == level)
                .WhereIf(!string.IsNullOrEmpty(searchKey), a => a.title.Contains(searchKey))
                .OrderBy(a => a.serId)
                .Page(1, 8)
                .ToList(a => new
                {
                    a.serId,
                    a.title,
                    parentName = _fsql.Select<YNDict>().Where(b => b.serId == a.parentId).First(b => b.title)
                });
            return list;
        }
    }
}
